<template>
  <div>
    <!-- 添加或修改资产清单表对话框 -->
    <el-dialog ref="editDialog" v-model="openFlag" title="申请" width="50%" append-to-body>
      <el-form ref="form" :model="formData" label-width="100" :rules="rules">
        <el-form-item label="借用人" prop="targetUserId">
          <user-select v-model="formData.targetUserId" v-model:name="formData.targetUserName" placeholder="请输入"></user-select>
        </el-form-item>
      </el-form>

      <el-form label-width="100">
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="applyData.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>


      <choose-table ref="chooseTable">
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="分类" prop="name"></el-table-column>
        <el-table-column label="品牌" prop="brand"></el-table-column>
        <el-table-column label="型号" prop="model"></el-table-column>
      </choose-table>

      <div slot="footer" class="dialog-footer">
        <el-row type="flex" justify="end">
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="sub">提交</el-button>
          <el-button @click="close">取 消</el-button>
        </el-row>

      </div>
    </el-dialog>

  </div>
</template>

<script setup>


import api from "@/api/asset/apply.js";
import UserSelect from "@/components/User/UserSelect.vue";
import ChooseTable from "@/components/Asset/ChooseTable.vue";

const emit = defineEmits(['sub']);

const props = defineProps({
  applyType: {
    type: String,
    required: true,
  }
})

// 是否编辑
const isEdit = ref(false);

const applyDataInit = {
  id: null,
  applyType: props.applyType,
  remark: null,
}

const applyData = reactive({ ...applyDataInit });

const formDataInit = {
  targetUserId: null,
  targetUserName: null,
  remark: null
}

let itemsDel = [];

const tableData = reactive({
  loading: false,
  list: [], // 表格列表数据
});


const openFlag = ref(false);
const form = ref();
const chooseTable = ref()

const emptyItem = {
  number: null,
  num: 1,
}

const formData = reactive({...formDataInit})

const rules = reactive({
  targetUserId: {
    required: true,message: '不能为空', trigger: 'blur',
  }
})

function reset() {
  Object.assign(applyData, applyDataInit);
  Object.assign(formData, formDataInit);
  tableData.list = [];
  itemsDel = [];
}

function open(row) {
  openFlag.value = true;
  reset();
  if (row) {
    api.detail(row).then(response=> {
      Object.assign(applyData, response);
      const resFormData = response.formData;
      if (resFormData) {
        Object.assign(formData, resFormData);
      }
      chooseTable.value.setList(response.relItems)

    })
    isEdit.value = true;
  } else {
    isEdit.value = false;
  }
}

function close() {
  openFlag.value = false;
}

function handleAdd() {
  tableData.list.push({...emptyItem});
}

function handleDelete({ row, $index }) {
  // 如果是编辑状态需要把删除的行存起来
  if (isEdit.value) {
    itemsDel.push(row);
  }
  tableData.list.splice($index, 1);
}

function getParams() {
  const apply = { ...applyData };
  apply.applyType = props.applyType;
  apply.formData = JSON.stringify({ ...formData });
  const items = chooseTable.value.getList();
  const itemsDel = chooseTable.value.getDeletedList();
  return {
    edit: isEdit.value,
    apply: apply,
    items: items,
    itemsDel: itemsDel
  }
}

function check() {
  return new Promise((resolve, reject) => {
    form.value.validate(async (valid) => {
      if (valid) {
        resolve();
      } else {
        reject();
      }
    });
  })
}

/** 保存 */
function save() {
  check().then(()=> {
    api.save(getParams()).then(response => {
      emit("sub");
      close();
    });
  });
}

function sub() {
  check().then(()=> {
    api.saveAndSub(getParams()).then(response => {
      emit("sub");
      close();
    });
  });

}

defineExpose({
  open
})

</script>

